package com.algebra.util.config;

import com.algebra.util.encrypt.EncryptorProperties;
import com.algebra.util.encrypt.InsertDataFieldFilter;
import com.algebra.util.encrypt.QueryDataFieldFilter;
import com.algebra.util.sensitive.ISensitiveStrategy;
import com.algebra.util.sensitive.SensitiveSerializer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;

/**
 * @author al
 * @date 2022/5/26 10:45
 * @description
 */
@Lazy
@Configuration(
        proxyBeanMethods = false
)
@EnableConfigurationProperties({EncryptorProperties.class})
public class AutoConfiguration  implements InitializingBean {

    @Resource
    private EncryptorProperties encryptorProperties;

    @Autowired(required = false)
    private ISensitiveStrategy sensitiveStrategy;

    @Bean
    @ConditionalOnMissingBean
    public QueryDataFieldFilter getDataFieldFilter() {
        return new QueryDataFieldFilter(this.encryptorProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    public InsertDataFieldFilter getInsertDataFieldFilter() {
        return new InsertDataFieldFilter(this.encryptorProperties);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (null != this.sensitiveStrategy) {
            SensitiveSerializer.setSensitiveStrategy(this.sensitiveStrategy);
        }
    }
}
